<?php

namespace app\api\controller\v1;

use app\api\validate\Count;
use app\api\validate\IDMustBePositiveInt;
use app\lib\exception\ProductException;
use app\api\model\Product as ProductModel;

class Product
{

	public function getRecent($count = 15)
	{
		(new Count())->goCheck();
		// return 'success';
		$products = ProductModel::getMostRecent($count);

		// if(!$products){
		if($products->isEmpty()){
			throw new ProductException();
		}

		// 方法1，临时隐藏掉一些字段,如'summary','stock'
		// $collection = collection($products);
		// // halt($collection);
		// // $products = $collection->hidden(['summary','stock']);
		// $products = $collection->hidden(['summary']);

		// 方法2，在database.php中定义直接返回数据集
		$products = $products->hidden(['summary']);

		return $products;
	}

	// public function getAllInCategory($id){  //在商品模型中，就不需要再写Product了。如果写名字又太长了
	public function getAllInCategory($id){
		(new IDMustBePositiveInt())->goCheck();
		$products = ProductModel::getProductsByCategoryID($id);
		if($products->isEmpty()){
			throw new ProductException();
		}
		$products = $products->hidden(['summary']);
		return $products;
	}
}